﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ZhimeiSoft.Framework.Client.Base;
using ZhimeiSoft.CharmManager.Client.App;
using ZhimeiSoft.CharmManager.Client.Manager;
using ZhimeiSoft.Framework.Client.Utils;
using ZhimeiSoft.CharmManager.Interface;
using ZhimeiSoft.CharmManager.DOL;

namespace ZhimeiSoft.CharmManager.Client.Forms
{
    public partial class CustomInfoView : BaseUserControl
    {
        /// <summary>
        /// 会员信息
        /// </summary>
        private DataTable dtCustomInfo = new DataTable();
        /// <summary>
        /// 店信息
        /// </summary>
        private DataSet dsSalon = new DataSet();

        public CustomInfoView()
        {
            InitializeComponent();
        }

        public CustomInfoView(CharmManagerApp _app)
            : base(_app) 
        {
            InitializeComponent();
            customSearch1.onSerchCustom = this.serchCustomInfo;
            customInfoSearch.onSerchCustom = this.serchCustomInfo;
            string error = string.Empty;
            BussinessProxyManager.Instance.QuerySalonInfo(out dsSalon, out error);

            ButtonItemIconHelper.InitRibbonBar(this.ribbonBar1);
            ButtonItemIconHelper.InitRibbonBar(this.ribbonBar2);
        }

        /// <summary>
        /// 搜索会员控件返回的会员信息
        /// </summary>
        /// <param name="drCustomInfo"></param>
        private void serchCustomInfo(DataRow drCustomInfo)
        {
            if (drCustomInfo == null)
            {
                return;
            }
            if (tabControl1.SelectedTabIndex == 0)
            {
                if (gdCustomList.DataSource == null)
                {
                    btnReferchCustomList_Click(null, null);
                }

                int idx = gvCustomList.LocateByValue(0, gridColumnCustomID, drCustomInfo[DBEntityCustomInfo.FieldName.CUSTOMERID]);
                if (idx != -1)
                {
                    gvCustomList.SelectRow(idx);
                    gvCustomList.FocusedRowHandle = idx;
                }
            }
            else
            {
                dtCustomInfo = drCustomInfo.Table.Clone();
                dtCustomInfo.ImportRow(drCustomInfo);
                grdCustomInfo.DataSource = this.dtCustomInfo;
            }
        }
        /// <summary>
        /// 回调函数触发刷新数据集操作
        /// </summary>
        private void refershCustomInfoData()
        {
            string error = string.Empty;
            customInfoSearch.refershCustomInfo(out error);
        }

        private void cvCustomInfo_CustomDrawCardFieldValue(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
        {
            if (e.Column.FieldName == DBEntityCustomInfo.FieldName.CSTATE)
            {
                Rectangle r = e.Bounds;
                string sText = DBEntityCustomInfo.ENUM_CSTATE.EnumStrToChineseStr(e.CellValue.ToString());
                e.Style.DrawString(e.Graphics, sText, r);
                e.Handled = true;
            }
            else if (e.Column.FieldName == DBEntityCustomInfo.FieldName.CBIRTHDAYTYPE)
            {
                Rectangle r = e.Bounds;
                string sText = DBEntityCustomInfo.ENUM_CBIRTHDAYTYPE.EnumStrToChineseStr(e.CellValue.ToString());
                e.Style.DrawString(e.Graphics, sText, r);
                e.Handled = true;
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            frmCustomInfoAdd frmCustom = new frmCustomInfoAdd(null);
            if (frmCustom.ShowDialog() == DialogResult.OK)
            {
                //刷新控件数据
                refershCustomInfoData();
            }
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (cvCustomInfo.FocusedRowHandle < 0)
            {
                MsgBox.MsgInfo("请选择正确的会员信息。");
                return;
            }
            DataRow drCustomInfo = cvCustomInfo.GetDataRow(cvCustomInfo.FocusedRowHandle);
            DBEntityCustomInfo customInfo = new DBEntityCustomInfo(drCustomInfo);

            frmCustomInfoAdd frmCustom = new frmCustomInfoAdd(customInfo);
            if (frmCustom.ShowDialog() == DialogResult.OK)
            {
                refershCustomInfoData();

                DataSet dsCustomInfo = new DataSet();
                string error = string.Empty;

                if (!BussinessProxyManager.Instance.QueryCustomerID(customInfo.SALONID, out dsCustomInfo, out error))
                {
                    MsgBox.MsgInfo(error);
                    return;
                }
                cvCustomInfo.BeginUpdate();
                dtCustomInfo = dsCustomInfo.Tables[0].Copy();
                grdCustomInfo.DataSource = dtCustomInfo;
                cvCustomInfo.EndUpdate();
            }
        }

        private void btnReferchCustomList_Click(object sender, EventArgs e)
        {
            DataSet dsCustom = new DataSet();
            string error = string.Empty;
            if (!BussinessProxyManager.Instance.QueryCustomState(string.Empty, out dsCustom, out error))
            {
                MsgBox.MsgInfo("获取用户信息失败：" + error);
                return;
            }
            gdCustomList.DataSource = dsCustom.Tables[0];
        }

        private void gvCustomList_DoubleClick(object sender, EventArgs e)
        {
            if (gvCustomList.FocusedRowHandle < 0)
            {
                MsgBox.MsgInfo("请选择正确的会员信息。");
                return;
            }
            DataRow drCustomInfo = gvCustomList.GetDataRow(gvCustomList.FocusedRowHandle);
            DBEntityCustomInfo customInfo = new DBEntityCustomInfo(drCustomInfo);
            frmCustomInfoAdd frmCustom = new frmCustomInfoAdd(customInfo);
            if (frmCustom.ShowDialog() == DialogResult.OK)
            {
                btnReferchCustomList_Click(sender, e);
            }
        }

        private void gvCustomList_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
        {
            if (e.Column.FieldName == DBEntityCustomInfo.FieldName.CSTATE)
            {
                Rectangle r = e.Bounds;
                string sText = DBEntityCustomInfo.ENUM_CSTATE.EnumStrToChineseStr(e.CellValue.ToString());
                e.Style.DrawString(e.Graphics, sText, r);
                e.Handled = true;
            }
            else if (e.Column.FieldName == DBEntityCustomInfo.FieldName.CBIRTHDAYTYPE)
            {
                Rectangle r = e.Bounds;
                string sText = DBEntityCustomInfo.ENUM_CBIRTHDAYTYPE.EnumStrToChineseStr(e.CellValue.ToString());
                e.Style.DrawString(e.Graphics, sText, r);
                e.Handled = true;
            }
        }

        private void buttonItem1_Click(object sender, EventArgs e)
        {
            gvCustomList_DoubleClick(sender, e);
        }

        private void buttonItem2_Click(object sender, EventArgs e)
        {
            DataSet dsCustom = new DataSet();
            string error = string.Empty;
            if (!BussinessProxyManager.Instance.QueryCustomState(string.Empty, out dsCustom, out error))
            {
                MsgBox.MsgInfo("获取用户信息失败：" + error);
                return;
            }
            grdCustomInfo.DataSource = dsCustom.Tables[0];
        }
    }
}
